#!/usr/bin/env python3

import sys
import os
from io import StringIO

from portconfig import get_port_config
from sonic_py_common import device_info
from sonic_py_common import multi_asic

# mock the redis for unit test purposes #
try:
    if os.environ["UTILITIES_UNIT_TESTING"] == "2":
        modules_path = os.path.join(os.path.dirname(__file__), "..")
        test_path = os.path.join(modules_path, "tests")
        sys.path.insert(0, modules_path)
        sys.path.insert(0, test_path)
        import mock_tables.dbconnector
        import mock_tables.mock_multi_asic
        mock_tables.dbconnector.load_namespace_config()
except KeyError:
    pass

def translate_line(line, ports):
    allowed_symbols = ['-', '_']
    sb = StringIO()
    start = 0
    end = 0
    while end < len(line):
        if line[end].isalnum() or line[end] in allowed_symbols:
            pass
        else: 
            # End of a word
            word = line[start:end]
            if word in ports:
                sb.write(ports[word]['alias'])
            else:
                sb.write(word)
            sb.write(line[end])
            start = end + 1
        end += 1
    if start != len(line):
        word = line[start:]
        if word in ports:
            sb.write(ports[word]['alias'])
        else:
            sb.write(word)
    return sb.getvalue()

def main():
    (platform, hwsku) = device_info.get_platform_and_hwsku()
    ports = {}
    for ns in multi_asic.get_namespace_list():
        (ports_ns, _, _) = get_port_config(hwsku=hwsku, platform=platform, asic_name=ns)
        ports.update(ports_ns)
    for line in sys.stdin:
        sys.stdout.write(translate_line(line, ports))

if __name__ == "__main__":
    main()
